﻿using System;
using System.Configuration;
using System.Reflection;
using DotNETStack.Core.Data;

namespace DotNETStack.Core.Configuration
{
    public static class IDataProviderConfigurationExtensions
    {
        public static IDataProvider GetInstance(this IDataProviderConfiguration config)
        {
            string[] typeParts = config.Type.Split(',');
            string typeName = Assembly.CreateQualifiedName(typeParts[1].Trim(), typeParts[0].Trim());
            Type type = Type.GetType(typeName);

            string connectionString =
                ConfigurationManager.ConnectionStrings[config.ConnectionStringName].ConnectionString;
            return Activator.CreateInstance(type, connectionString) as IDataProvider;
        }
    }
}